﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using SilverlightApplication1;
using SilverlightApplication1.DataTypesUI;
using System.Globalization;

namespace SilverlightApplication1
{
    public partial class AdvancedSearch : UserControl
    {
        private TextLanguage textL;

        public AdvancedSearch()
        {
            InitializeComponent();

            //By default: English
            textL = new TextLanguage();

            //Select default condition: ALL
            cmbFilCondition.SelectedIndex = 0;
        }

        DTSearchCriteria advanced = new DTSearchCriteria();


        #region Gets

        //Author
        public string GetAuthor
        {            
            get { return txtFilAuthor.Text.Trim(); }
        }

        //Publisher
        public string GetPublisher
        {
            get { return txtFilPublisher.Text.Trim(); }
        }

        //Title
        public string GetTitle
        {
            get { return txtFilTitle.Text.Trim(); }
        }

        //Keyword
        public string GetKeyword
        {
            get { return txtFilKeyword.Text.Trim(); }
        }

        //Condition
        public string GetCondition
        {
            get
            {
                string Aux = "";

                switch (cmbFilCondition.SelectedIndex)
                {
                    case 1:
                        Aux = DTSearchCriteria.New;
                        break;
                    //Used
                    case 2:
                        Aux = DTSearchCriteria.Used;
                        break;
                    //Collectible
                    case 3:
                        Aux = DTSearchCriteria.Collectible;
                        break;
                    //Refurbished
                    case 4:
                        Aux = DTSearchCriteria.Refurbished;
                        break;

                    //All
                    default:
                        Aux = DTSearchCriteria.All;
                        break;
                }

                return Aux;
            }
        }

        //Category
        public int GetCategory
        {
            get
            {
                if (cmbFilCategory.SelectedIndex == -1)
                {
                    return DTSearchCriteria.Nothing;
                }
                else
                {
                    return int.Parse(((ComboBoxItem)cmbFilCategory.SelectedItem).Tag.ToString(),CultureInfo.CurrentCulture);
                }
            }
        }

        //Max Price
        public int GetMaxPrice
        {
            get
            {
                if (cmbFilMaxPrice.SelectedIndex == -1)
                {
                    return 0;
                }
                else
                {
                    return int.Parse(((ComboBoxItem)cmbFilMaxPrice.SelectedItem).Content.ToString(),CultureInfo.CurrentCulture);
                }
            }
        }

        //Min Price
        public int GetMinPrice
        {
            get
            {
                if (cmbFilMinPrice.SelectedIndex == -1)
                {
                    return 0;
                }
                else
                {
                    return int.Parse(((ComboBoxItem)cmbFilMinPrice.SelectedItem).Content.ToString(),CultureInfo.CurrentCulture);
                }
            }
        }


        //Sor By

        //First
        public int GetSortFirst
        {
            get
            {
                int Aux = 0;

                switch (cmbSortFirst.SelectedIndex)
                {
                    case -1:
                        Aux = 0;
                        break;
                    case 0:
                        Aux = (hlkSortOrderF.Tag.ToString() == "asc") ? DTSearchCriteria.Ascending: DTSearchCriteria.Descending;
                        break;
                    case 1:
                        Aux = DTSearchCriteria.PubDate;
                        break;
                    case 2:
                        Aux = DTSearchCriteria.Tit;
                        break;
                }

                return Aux;
            }
        }
        //Second
        public int GetSortSecond
        {
            get
            {
                int Aux = 0;

                switch (cmbSortSecond.SelectedIndex)
                {
                    case -1:
                        Aux = 0;
                        break;
                    case 0:
                        Aux = (hlkSortOrderS.Tag.ToString() == "asc") ? DTSearchCriteria.Ascending: DTSearchCriteria.Descending;
                        break;
                    case 1:
                        Aux = DTSearchCriteria.PubDate;
                        break;
                    case 2:
                        Aux = DTSearchCriteria.Tit;
                        break;
                }

                return Aux;
            }
        }
        //Third
        public int GetSortThird
        {
            get
            {
                int Aux = 0;

                switch (cmbSortThird.SelectedIndex)
                {
                    case -1:
                        Aux = 0;
                        break;
                    case 0:
                        Aux = (hlkSortOrderT.Tag.ToString() == "asc") ? DTSearchCriteria.Ascending: DTSearchCriteria.Descending;
                        break;
                    case 1:
                        Aux = DTSearchCriteria.PubDate;
                        break;
                    case 2:
                        Aux = DTSearchCriteria.Tit;
                        break;
                }

                return Aux;
            }
        }

        #endregion


        public void SetFocus()
        {
            txtFilAuthor.Focus();
        }

        #region Clean Filds

        private void CleanFields()
        {
            txtFilAuthor.Text = "";
            txtFilPublisher.Text = "";
            txtFilTitle.Text = "";
            txtFilKeyword.Text = "";

            //Select default condition: All
            cmbFilCondition.SelectedIndex = 0;

            cmbFilCategory.SelectedIndex = -1;
            cmbFilMaxPrice.SelectedIndex = -1;
            cmbFilMinPrice.SelectedIndex = -1;

            cmbSortThird.SelectedIndex = -1;
            cmbSortSecond.SelectedIndex = -1;
            cmbSortFirst.SelectedIndex = -1;

        }

        private void hlkClean_Click(object sender, RoutedEventArgs e)
        {
            CleanFields();
            txtFilAuthor.Focus();

        }

        #endregion

        #region GotFocus

        private void txtFilText_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox txtAux = ((TextBox)sender);
            txtAux.SelectionStart = 0;
            txtAux.SelectionLength = txtAux.Text.Length;
        }

        #endregion

        #region Language

        public void SetLanguage(int language)
        {
            textL.IdentifyLanguage = language;
            int Aux;

            lblAdvTitle.Text = textL.GetText(Field.AdvancedTitle);
            lblAdvSort.Text = textL.GetText(Field.AdvancedSortBy);
            lblAdvFiltered.Text = textL.GetText(Field.AdvancedFilteredBy);
            lblFilAuthor.Text = textL.GetText(Field.FilterAuthor);
            lblFilPublisher.Text = textL.GetText(Field.FilterPublisher);
            lblFilTitle.Text = textL.GetText(Field.FilterTitle);
            lblfilCategory.Text = textL.GetText(Field.FilterCategory);
            //Category
            Aux = cmbFilCategory.SelectedIndex;
            ((ComboBoxItem)cmbFilCategory.Items[0]).Content =
                textL.GetText(Field.FilterLiteratureFiction);
            ((ComboBoxItem)cmbFilCategory.Items[1]).Content =
                textL.GetText(Field.FilterMedicine);
            ((ComboBoxItem)cmbFilCategory.Items[2]).Content =
                textL.GetText(Field.FilterMysteryThrillers);
            ((ComboBoxItem)cmbFilCategory.Items[3]).Content =
                textL.GetText(Field.FilterNonfiction);
            ((ComboBoxItem)cmbFilCategory.Items[4]).Content =
                textL.GetText(Field.FilterNature);
            if (Aux != -1)
            {
                cmbFilCategory.SelectedIndex = -1;
                cmbFilCategory.SelectedIndex = Aux;
            }

            //Condition
            Aux = cmbFilCondition.SelectedIndex;
            lblFilCondition.Text = textL.GetText(Field.FilterConditions);
            ((ComboBoxItem)cmbFilCondition.Items[0]).Content =
                textL.GetText(Field.FilterConditionAll);
            ((ComboBoxItem)cmbFilCondition.Items[1]).Content =
                textL.GetText(Field.FilterConditionNew);
            ((ComboBoxItem)cmbFilCondition.Items[2]).Content =
                textL.GetText(Field.FilterConditionUsed);
            ((ComboBoxItem)cmbFilCondition.Items[3]).Content =
                textL.GetText(Field.FilterConditionCollectible);
            ((ComboBoxItem)cmbFilCondition.Items[4]).Content =
                textL.GetText(Field.FilterConditionRefurbished);
            cmbFilCondition.SelectedIndex = -1;
            cmbFilCondition.SelectedIndex = Aux;

            lblFilKeyword.Text = textL.GetText(Field.FilterKeyword);
            lblFilMaxPrice.Text = textL.GetText(Field.FilterMaximumPrice);
            lblFilMinPrice.Text = textL.GetText(Field.FilterMinimumPrice);


            //Sort By
            lblSortFirst.Text = textL.GetText(Field.AdvancedSortFirst);
            lblSortSecond.Text = textL.GetText(Field.AdvancedSortSecond);
            lblSortThird.Text = textL.GetText(Field.AdvancedSortThird);

            int AuxS, AuxT;

            Aux = cmbSortFirst.SelectedIndex;
            AuxS = cmbSortSecond.SelectedIndex;
            AuxT = cmbSortThird.SelectedIndex;

            //Sort First
            ((ComboBoxItem)cmbSortFirst.Items[0]).Content =
                textL.GetText(Field.AdvancedSortListPrice);
            ((ComboBoxItem)cmbSortFirst.Items[1]).Content =
                textL.GetText(Field.AdvancedSortPublication);
            ((ComboBoxItem)cmbSortFirst.Items[2]).Content =
                textL.GetText(Field.AdvancedSortTitle);
            if (Aux != -1)
            {
                cmbSortFirst.SelectedIndex = -1;
                cmbSortFirst.SelectedIndex = Aux;
            }

            //Sort Second            
            ((ComboBoxItem)cmbSortSecond.Items[0]).Content =
                textL.GetText(Field.AdvancedSortListPrice);
            ((ComboBoxItem)cmbSortSecond.Items[1]).Content =
                textL.GetText(Field.AdvancedSortPublication);
            ((ComboBoxItem)cmbSortSecond.Items[2]).Content =
                textL.GetText(Field.AdvancedSortTitle);
            cmbSortSecond.SelectedIndex = AuxS;

            //Sort Third            
            ((ComboBoxItem)cmbSortThird.Items[0]).Content =
                textL.GetText(Field.AdvancedSortListPrice);
            ((ComboBoxItem)cmbSortThird.Items[1]).Content =
                textL.GetText(Field.AdvancedSortPublication);
            ((ComboBoxItem)cmbSortThird.Items[2]).Content =
                textL.GetText(Field.AdvancedSortTitle);
            cmbSortThird.SelectedIndex = AuxT;

            #region ToolTip Order By
            //ascending
            imgSortOrderF.Tag = textL.GetText(Field.AdvancedSortPriceAscending);
            //descending
            imgSortOrderS.Tag = textL.GetText(Field.AdvancedSortPriceDescending);

            HyperlinkButton hlkAux = null;

            if (hlkSortOrderF.Visibility == Visibility.Visible)
            {
                hlkAux = hlkSortOrderF;
            }
            else if (hlkSortOrderS.Visibility == Visibility.Visible)
            {
                hlkAux = hlkSortOrderS;
            }
            else if (hlkSortOrderT.Visibility == Visibility.Visible)
            {
                hlkAux = hlkSortOrderT;
            }

            if (hlkAux != null)
            {
                string textAux;
                if (hlkAux.Tag.ToString() == "asc")
                {
                    textAux = imgSortOrderF.Tag.ToString();
                }
                else
                {
                    textAux = imgSortOrderS.Tag.ToString();
                }

                ToolTipService.SetToolTip(hlkAux, textAux);
            }

            #endregion

            //Clean
            hlkClean.Content = textL.GetText(Field.AdvancedClean);


        }

        #endregion

        private void cmbSort_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int FirstIndex = cmbSortFirst.SelectedIndex;
            int SecondIndex = cmbSortSecond.SelectedIndex;

            //ComboBox SortSecond 
            if ((FirstIndex == -1 || FirstIndex == cmbSortSecond.SelectedIndex) && (cmbSortSecond.SelectedIndex != -1))
            {
                ((ComboBoxItem)cmbSortSecond.Items[cmbSortSecond.SelectedIndex]).IsSelected = false;
                cmbSortSecond.SelectedIndex = -1;
            }

            //ComboBox SortThird
            if ((FirstIndex == -1 || SecondIndex == -1 || FirstIndex == cmbSortThird.SelectedIndex || SecondIndex == cmbSortThird.SelectedIndex) && (cmbSortThird.SelectedIndex != -1))
            {
                ((ComboBoxItem)cmbSortThird.Items[cmbSortThird.SelectedIndex]).IsSelected = false;
                cmbSortThird.SelectedIndex = -1;
            }
            hlkSortOrderF.Visibility = (cmbSortFirst.SelectedIndex == 0) ? Visibility.Visible : Visibility.Collapsed;
            hlkSortOrderS.Visibility = (cmbSortSecond.SelectedIndex == 0) ? Visibility.Visible : Visibility.Collapsed;
            hlkSortOrderT.Visibility = (cmbSortThird.SelectedIndex == 0) ? Visibility.Visible : Visibility.Collapsed;

            HyperlinkButton hlkAux = null;

            if (hlkSortOrderF.Visibility == Visibility.Visible)
            {
                hlkAux = hlkSortOrderF;
            }
            else if (hlkSortOrderS.Visibility == Visibility.Visible)
            {
                hlkAux = hlkSortOrderS;
            }
            else if (hlkSortOrderT.Visibility == Visibility.Visible)
            {
                hlkAux = hlkSortOrderT;
            }

            if (hlkAux != null)
            {
                hlkAux.Tag = hlkAux.Tag.ToString() == "asc" ? "desc" : "asc";
                hlkSortOrder_Click(hlkAux, null);
            }


        }

        private void hlkSortOrder_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hlkAux = ((HyperlinkButton)sender);
            Image imgAux = ((Image)hlkAux.Content);

            if (hlkAux.Tag.ToString() == "asc")
            {
                imgAux.Source = new BitmapImage(new Uri("images/Orderdesc.jpg", UriKind.RelativeOrAbsolute));
                ToolTipService.SetToolTip(hlkAux, imgSortOrderS.Tag.ToString());
                hlkAux.Tag = "desc";
            }
            else
            {
                imgAux.Source = new BitmapImage(new Uri("images/Orderasc.jpg", UriKind.RelativeOrAbsolute));
                ToolTipService.SetToolTip(hlkAux, imgSortOrderF.Tag.ToString());
                hlkAux.Tag = "asc";
            }

            switch (hlkAux.Name)
            {
                case "hlkSortOrderF":
                    cmbSortFirst.Focus();
                    break;
                case "hlkSortOrderS":
                    cmbSortSecond.Focus();
                    break;
                case "hlkSortOrderT":
                    cmbSortThird.Focus();
                    break;
            }

        }

        private void cmbFilGeneric_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Delete)
            {
                ((ComboBox)sender).SelectedIndex = -1;
            }
        }



    }
}
